﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ReadPointsRingNumberExcel;
using Newtonsoft.Json.Linq;
using MetroDataApi.Models;

namespace MetroDataApi.Controllers.Management
{
    public class RingNumberConfigController : ApiController
    {


        public bool Get(int prjId)
        {
            using (var db = new MetroDataDataContext())
            {
                var l = db.MonitorPointsDictionary.Where(p =>
                    p.PrjId == prjId && p.RingNumber != null && p.RingNumber != "").Count();
                if (l > 0) return true;
                else return false;
            }
        }

        // POST api/ringnumberconfig
        public bool Post([FromBody]string value)
        {
            try
            {
                JObject jsonData = JObject.Parse(value);
                var url = jsonData["url"].ToString();
                var fileName = jsonData["fileName"].ToString();
                int prjId = jsonData["prjId"].ToObject<int>();
                RingConfigReadHelper.ReadFromRemote(prjId, url, fileName);
                return true;
            }
            catch
            {
                return false;
            }
        }

        // PUT api/ringnumberconfig/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/ringnumberconfig/5
        public void Delete(int id)
        {
        }
    }

    public class RingConfigReadHelper
    {
        public static void ReadFromRemote(int prjId, string remoteUri, string fileName)
        {
            using (WebClient Client = new WebClient())
            {
                var localPath = System.Web.HttpContext.Current.Server.MapPath(@"~/Excels");
                localPath += @"\" + fileName;
                Client.DownloadFile(remoteUri, localPath);

                ReadExcel read = new ReadExcel(localPath);
                var list = read.ReadFile();

                using (var db = new MetroDataDataContext())
                {

                    var allMonitorPointsDictionary = (from d in db.MonitorPointsDictionary
                                                      where d.PrjId == prjId
                                                      select d).ToList();
                    var existed = (from l in list
                                   join p in allMonitorPointsDictionary
                                   on l.Name equals p.Name into g
                                   from d in g.DefaultIfEmpty()
                                   where d != null
                                   select new
                                   {
                                       Item = d,
                                       Ring = l.RingNumber,
                                       Mile = l.Mile
                                   }).ToList();

                    existed.ForEach(e =>
                    {
                        e.Item.RingNumber = e.Ring;
                        e.Item.Mileage = e.Mile;
                    });



                    //原有的ringNumber表继续修改
                    var oldRingNumbers = from d in db.PointsRingNumbers
                                         where d.PrjId == prjId
                                         select d;
                    db.PointsRingNumbers.DeleteAllOnSubmit(oldRingNumbers);
                    var newRingNumbers = from l in list
                                         select new PointsRingNumbers()
                                             {
                                                 PrjId = prjId,
                                                 RingNumber = l.RingNumber,
                                                 PointName = l.Name,
                                                 Mileage = l.Mile

                                             };
                    db.PointsRingNumbers.InsertAllOnSubmit(newRingNumbers);

                    db.SubmitChanges();
                }
            }
        }



    }
}
